use std::string::String;
use salvo::http::ParseError;
use salvo::prelude::Json;
use serde::Serialize;
use validator::{ValidationErrors};
use crate::domain::error::AppResult;
use crate::domain::error::AccountError;

pub mod user_dto;
pub mod login_dto;
pub mod biz_dto;
pub mod project_dto;
pub mod group_dto;
pub mod module_dto;
pub mod fields_dto;
mod validate;

/// Res 请求响应
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct ResObj<T> {
    pub code: i32,
    pub data: Option<T>,
    pub message: String,
    #[serde(rename(serialize = "isSuccess"))]
    pub is_success: bool,
    pub error: Option<String>,
}

/// PageRes 分页请求
#[derive(Debug, Serialize)]
pub(crate) struct PageRes<T> {
    pub page: u64,
    pub limit: u64,
    pub total: u64,
    pub row: Vec<T>,
}

pub(crate) type Res<T> = Result<Json<ResObj<T>>, Json<ResObj<()>>>;

impl<T> ResObj<T> {
    pub fn ok(data: Option<T>) -> Json<Self> {
        Json(Self {
            code: 200,
            message: "操作成功".to_string(),
            is_success: true,
            data,
            error: None,
        })
    }

    #[allow(dead_code)]
    pub fn fail(data: String) -> Json<Self> {
        Json(Self {
            code: 500,
            message: data.to_string(),
            is_success: false,
            data: None,
            error: None,
        })
    }

    pub fn fail_with_error(data: AccountError) -> Json<Self> {
        Json(Self {
            code: 500,
            message: data.to_string(),
            is_success: false,
            data: None,
            error: None,
        })
    }

    #[allow(dead_code)]
    pub fn fail_with_parser(err: ParseError) -> Json<Self> {
        Json(ResObj {
            code: 500,
            message: format!("表单验证失败: {}", err),
            is_success: false,
            data: None,
            error: None,
        })
    }
}

/// result_to_custom 自定义响应
#[allow(dead_code)]
pub fn result_to_custom<T>(code: i32, msg: String) -> Json<ResObj<()>> {
    Json(ResObj{
        code,
        data: None,
        message: msg,
        is_success: false,
        error: None,
    })
}

#[allow(dead_code)]
pub fn result_to_res<T>(result: AppResult<T>) -> Res<T> {
    result.map(|data| Json(ResObj {
        code: 200,
        data: Some(data),
        message: "成功".to_string(),
        is_success: true,
        error: None,
    })).map_err(|err| Json(ResObj {
        code: 500,
        data: None,
        message: err.to_string(),
        is_success: false,
        error: None,
    }))
}

#[allow(dead_code)]
pub fn rbatis_to_res<T>(value: rbatis::Result<T>) -> Res<T> {
    value.map(|data| Json(ResObj {
        code: 200,
        data: Some(data),
        message: "操作成功".to_string(),
        is_success: true,
        error: None,
    })).map_err(|err| Json(ResObj {
        code: 500,
        data: None,
        message: err.to_string(),
        is_success: false,
        error: None,
    }))
}


/// validation_to_res 表单验证转响应实体
#[allow(dead_code)]
pub fn validation_to_res<T>(err: ValidationErrors) -> Res<T> {
    Err(Json(ResObj {
        code: 400,
        data: None,
        message: format!("表单验证失败: {}", err),
        is_success: false,
        error: None,
    }))
}

/// validation_to_res 表单验证转响应实体
#[allow(dead_code)]
pub fn validation_to_json<T>(err: ValidationErrors) -> Json<ResObj<T>> {
    Json(ResObj {
        code: 400,
        data: None,
        message: format!("表单验证失败: {}", err),
        is_success: false,
        error: None,
    })
}

/// validation_to_res 表单验证转响应实体
#[allow(dead_code)]
pub fn parser_to_res<T>(err: ParseError) -> Res<T> {
    Err(Json(ResObj {
        code: 400,
        data: None,
        message: format!("表单验证失败: {}", err),
        is_success: false,
        error: None,
    }))
}

/// validation_to_res 表单验证转响应实体
#[allow(dead_code)]
pub fn parser_to_json<T>(err: ParseError) -> Json<ResObj<T>> {
    Json(ResObj {
        code: 400,
        data: None,
        message: format!("表单验证失败: {}", err),
        is_success: false,
        error: None,
    })
}


